﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using DIST.LR.ZHJG.WCFServices.EntityDataManageService;
using System.Collections.Generic;
using DIST.LR.ZHJG.DataModel;
using DIST.LR.ZHJG.Events;

namespace DIST.LR.ZHJG.Pgybc
{
    public class Zbgl_TreeModel : INotifyPropertyChanged
    {
        IList<TKPITYPE> lst1;
        IList<TKPI> lst2;
        protected EntityDataManageServiceClient client = DIST.LR.ZHJG.WCFServices.CommonServices.WCFService;

        /// <summary>
        /// 获取指标类型，一级指标
        /// </summary>
        public void GetKPITypeMonitorData()
        {
            try
            {
                string strSql = string.Format("from TKPITYPE order by ID asc");
                client.SelectTkpiTypeCompleted += new EventHandler<SelectTkpiTypeCompletedEventArgs>(client_SelectTkpiTypeCompleted);
                client.SelectTkpiTypeAsync(strSql, "0");

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void client_SelectTkpiTypeCompleted(object sender, SelectTkpiTypeCompletedEventArgs e)
        {
            lst1 = e.Result as IList<TKPITYPE>;
            GetKpiMonitorData();    
        }

        /// <summary>
        /// 获取具体指标名称，二级指标
        /// </summary>
        public void GetKpiMonitorData()
        {
            try
            {
                string strSql = string.Format("from TKPI order by ID asc");
                client.SelectTkpiCompleted += new EventHandler<SelectTkpiCompletedEventArgs>(client_SelectTkpiCompleted);
                client.SelectTkpiAsync(strSql, "0");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void client_SelectTkpiCompleted(object sender, SelectTkpiCompletedEventArgs e)
        {
            lst2 = e.Result as IList<TKPI>;
            GetTreeDataSource(lst1, lst2);
        }

        /// <summary>
        /// 构造RadTreeView的数据源
        /// </summary>
        /// <param name="list1">一级</param>
        /// <param name="list2">二级</param>
        void GetTreeDataSource(IList<TKPITYPE> list1, IList<TKPI> list2)
        {
            ObservableCollection<KPIType> t_KPTypeDataSource = new ObservableCollection<KPIType>();
            KPIType kpitype;
            for (int i = 0; i < list1.Count; i++)
            {
                kpitype = new KPIType();
                int id = Convert.ToInt32(list1[i].ID);
                string kpitypeName = list1[i].NAME.ToString();
                ObservableCollection<KPI> kpiModels = new ObservableCollection<KPI>();
                KPI kpi;
                foreach (TKPI tkpi in list2)
                {
                    kpi = new KPI();
                    if (tkpi.TYPEID == id)
                    {
                        kpi.KPIId = tkpi.ID;
                        kpi.KPIName = tkpi.NAME;
                        kpi.KPIDescption = tkpi.DESCPTION;
                        kpi.KPIOriginid = tkpi.ORIGINID;
                        kpi.KPITypeid = tkpi.TYPEID;
                        kpi.KPIUnit = tkpi.UNIT;
                        kpiModels.Add(kpi);
                    }
                }
                kpitype.Id = id;
                kpitype.Name = kpitypeName;
                if (kpiModels != null)
                {
                    kpitype.KPIs = kpiModels;
                }
                t_KPTypeDataSource.Add(kpitype);              
            }
            KPTypeDataSource = t_KPTypeDataSource;
            EventAggregatorRepository.EventAggregator
                    .GetEvent<GridKpiTypeEvent>()
                    .Publish(t_KPTypeDataSource);
        }

        public Zbgl_TreeModel()
        {
            GetKPITypeMonitorData();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private ObservableCollection<KPIType> _KPTypeDataSource;
        public ObservableCollection<KPIType> KPTypeDataSource
        {
            get
            {
                return this._KPTypeDataSource;
            }
            set
            {
                if (this._KPTypeDataSource != value)
                {
                    this._KPTypeDataSource = value;
                    this.OnPropertyChanged("KPTypeDataSource");
                }
            }
        }

    }
}
